package com.md.base.exception;

import com.md.base.EnumBase;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 刘骄阳
 * 2022-04-08 15:39
 */
@Getter
@AllArgsConstructor(staticName = "of")
@Slf4j
public class ServerException extends RuntimeException {
  private final EnumBase error;

  public static ServerException build(final EnumBase error, final Object... value) {
    return ServerException.of(ErrorMessage.of(error.getCode(), String.format(error.getMessage(), value)));
  }

  public static ServerException build(final EnumBase error, final Exception e, final Object... value) {
    log.error("发生异常: {}", e.getMessage(), e);
    return ServerException.of(ErrorMessage.of(error.getCode(), String.format(error.getMessage(), value)));
  }

  public static ServerException build(final Integer code, final String message) {
    return ServerException.of(ErrorMessage.of(code, message));
  }

  /**
   * 返回错误信息
   */
  @Override
  public String getMessage() {
    return error.getMessage();
  }

  /**
   * 返回错误信息
   */
  public Integer getCode() {
    return error.getCode();
  }
}
